package com.simon.train.batch.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.simon.train.batch.feign.BusinessFeign;
import com.simon.train.common.Result.Result;
import jakarta.annotation.Resource;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.Date;

@DisallowConcurrentExecution  //不允许并发执行
public class DailyTrainJob implements Job {

    private static final Logger LOG = LoggerFactory.getLogger(DailyTrainJob.class);

    @Resource
    BusinessFeign businessFeign;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        /**
         * 由于定时任务是系统发起的 不经过请求
         * 所以SpringMvcConfig的LogInterceptor捕捉不到
         * 所以可以直接自行添加进来
         */
        MDC.put("LOG_ID", System.currentTimeMillis() + RandomUtil.randomString(3));
        LOG.info("生成15天后的车次数据 开始");
        Date now = new Date();
        DateTime nowAfter15Day = DateUtil.offsetDay(now, 15);
        Date nowAfter15 = nowAfter15Day.toJdkDate();
        Result<Object> objectResult = businessFeign.GenerateDaily(nowAfter15);
        LOG.info("生成15天后的车次数据 结束 结果是:{}",objectResult);
    }
}
